package email

import (
	"fmt"
	"strconv"
	"sync"
	"gopkg.in/gomail.v2"
)

func SendMail(mailTo string, subject string, body string,attach string, wg *sync.WaitGroup) error {
	defer wg.Done()
	// mailConn := map[string]string{
	//     "username": "15268144812@139.com",
	//     "authCode": "ed777d226c4f836a7700",
	//     "host":     "smtp.139.com",
	//     "port":     "465",
	// }
	mailConn := map[string]string{
		"username": "1612346638@qq.com",
		"authCode": "yndmqodzofubdfjg",
		"host":     "smtp.qq.com",
		"port":     "465",
	}
	port, _ := strconv.Atoi(mailConn["port"])
	m := gomail.NewMessage()
	m.SetHeader("From", "Iron will"+"<"+mailConn["username"]+">")
	m.SetHeader("To", mailTo)
	m.SetHeader("Subject", subject)
	m.SetBody("text/html", body)
	if attach!=""{
		m.Attach(attach)
	}
	d := gomail.NewDialer(mailConn["host"], port, mailConn["username"], mailConn["authCode"])
	err := d.DialAndSend(m)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("Send Email Successfully!")
	}
	return err
}
